package com.springboot.cloud.demos.producer.rest;

import com.springboot.cloud.common.core.entity.vo.Result;
import com.springboot.cloud.common.web.redis.CacheDuration;
import com.springboot.cloud.common.web.redis.ExpireTimeConstant;
import com.springboot.cloud.demos.entity.form.MomentCommentForm;
import com.springboot.cloud.demos.entity.param.MomentCommentQueryParam;
import com.springboot.cloud.demos.entity.po.Moment;
import com.springboot.cloud.demos.entity.po.MomentComment;
import com.springboot.cloud.demos.producer.service.IMomentCommentService;
import com.springboot.cloud.demos.producer.service.IMomentService;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/momentComment")
@Api("momentComment")
@Slf4j
@CacheDuration(duration = ExpireTimeConstant.ONE_DAY)
public class MomentCommentController {

    @Autowired
    private IMomentCommentService momentCommentService;
    @Autowired
    private IMomentService momentService;

    @ApiOperation(value = "新增产品", notes = "新增一个产品")
    @ApiImplicitParam(name = "userMomentCommentForm", value = "新增产品form表单", required = true, dataType = "MomentCommentForm")
    @PostMapping
    public Result add(@Valid @RequestBody MomentCommentForm momentCommentForm) {
        log.info("name:{}", momentCommentForm);
        MomentComment userMomentComment = momentCommentForm.toPo(MomentComment.class);
        //同时更新评论数
        Moment moment = momentService.get(momentCommentForm.getMomentId());
        moment.setCommentNum(moment.getCommentNum()+1);
        momentService.update(moment);
        return Result.success(momentCommentService.add(userMomentComment));
    }

    @ApiOperation(value = "删除产品", notes = "根据url的id来指定删除对象")
    @ApiImplicitParam(paramType = "path", name = "id", value = "产品ID", required = true, dataType = "long")
    @DeleteMapping(value = "/{id}")
    public Result delete(@PathVariable String id) {
        return Result.success(momentCommentService.delete(id));
    }

    @ApiOperation(value = "修改产品", notes = "修改指定产品信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "产品ID", required = true, dataType = "long"),
            @ApiImplicitParam(name = "userMomentCommentForm", value = "产品实体", required = true, dataType = "MomentCommentForm")
    })
    @PutMapping(value = "/{id}")
    public Result update(@PathVariable String id, @Valid @RequestBody MomentCommentForm userMomentCommentForm) {
        MomentComment userMomentComment = userMomentCommentForm.toPo(MomentComment.class);
        userMomentComment.setId(id);
        return Result.success(momentCommentService.update(userMomentComment));
    }

    @ApiOperation(value = "获取产品", notes = "获取指定产品信息")
    @ApiImplicitParam(paramType = "path", name = "id", value = "产品ID", required = true, dataType = "long")
    @GetMapping(value = "/{id}")
    public Result get(@PathVariable String id) {
        log.info("get with id:{}", id);
        return Result.success(momentCommentService.get(id));
    }

    @ApiOperation(value = "查询产品", notes = "根据条件查询产品信息，简单查询")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "momentId", value = "momentId", required = true, dataType = "string"),
            @ApiImplicitParam(name = "pageNo", value = "起始页", required = true, dataType = "int"),
            @ApiImplicitParam(name = "PageSize", value = "页大小", required = true, dataType = "int")
    })
    @ApiResponses(
            @ApiResponse(code = 200, message = "处理成功", response = Result.class)
    )
    @GetMapping("/query")
    public Result query(@RequestParam String momentId, @RequestParam Integer pageNo, @RequestParam Integer pageSize) {
        return Result.success(momentCommentService.query(momentId, pageNo, pageSize));
    }

    @ApiOperation(value = "查询数量", notes = "根据条件查询产品信息，简单查询")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "momentId", value = "momentId", required = true, dataType = "string"),
    })
    @ApiResponses(
            @ApiResponse(code = 200, message = "处理成功", response = Result.class)
    )
    @GetMapping("/queryCount")
    public Result queryCount(@RequestParam String momentId) {
        return Result.success(momentCommentService.queryCount(momentId));
    }
}

